//
//  TDNewfeatureViewController.swift
//  微博
//
//  Created by tong dan on 16/3/8.
//  Copyright © 2016年 tong dan. All rights reserved.
//

import UIKit

class TDNewfeatureViewController: UIViewController {
    
    override func viewDidLoad() {
        
        super.viewDidLoad()
        
        setupUI()
    }
    
    
    private func setupUI(){
        
        
        //添加View
        view.addSubview(scrollView)
        view.addSubview(pageControl)
        
        
        //添加约束
        scrollView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(UIEdgeInsetsZero)
        }
        pageControl.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.view)
            make.bottom.equalTo(self.view.snp_bottom).offset(-80)
        }
        
        
        //定义总共页数
        let count = 4
        //设置pagecontrol 总页数
        pageControl.numberOfPages = count
        //循环添加图片
        for i in 0..<count {
            let imageView = UIImageView(image: UIImage(named: "new_feature_\(i+1)"))
            
            //设置imageView的frame
            imageView.frame = CGRect(x: CGFloat(i) * SCREENW, y: 0, width: SCREENW, height: SCREENH)
            
            scrollView.addSubview(imageView)
            
            //判断，如果是最后一页，设置最后一页的内容
            if i == count-1 {
            
            setUpLastPage(imageView)
            
            }
            
        }
        //设置scrollView contensize 让其滑动
        scrollView.contentSize = CGSize(width: CGFloat(count) * SCREENW, height: 0)
    }
    
    //设置最后一页的内容
    private func setUpLastPage(imageView:UIImageView){
    
        //开启用户交互
        imageView.userInteractionEnabled = true
    //添加控件
        imageView.addSubview(enterButton)
        imageView.addSubview(sharedButton)
    
    //添加控件约束
        sharedButton.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(imageView)
            make.bottom.equalTo(imageView.snp_bottom).offset(-150)
        }
        enterButton.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(sharedButton)
            make.top.equalTo(sharedButton.snp_bottom).offset(5)
        }
    }
    
    // 监听
    @objc private func enterButtonClick(){
    //跳转界面 -->首页
        print("跳转界面")
        NSNotificationCenter.defaultCenter().postNotificationName(TDSwitchRootVCNotification, object: nil)
        
    }
    
    @objc private func shareButtonClick(button:UIButton){
    
    button.selected = !button.selected
    
    }
   
    //懒加载控件
    private lazy var enterButton: UIButton = {
    let button = UIButton()
        
        //设置点击事件
        button.addTarget(self, action: "enterButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        //设置不同状态下的背景
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        button.setTitle("进入微博", forState: UIControlState.Normal)
    
    return button
    
    }()
    
    //懒加载分享按钮
    private lazy var sharedButton: UIButton = {
    let button = UIButton()
        
        //设置点击事件
        button.addTarget(self, action: "shareButtonClick:", forControlEvents: UIControlEvents.TouchUpInside)
    //设置文字颜色大小和内容
        button.setTitleColor(UIColor.grayColor(), forState: UIControlState.Normal)
        button.setTitle("分享到微博", forState: UIControlState.Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(16)
    
    
    //设置不同状态下的图片
        button.setImage(UIImage(named: "new_feature_share_false"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "new_feature_share_true"), forState: UIControlState.Selected)
        button.titleEdgeInsets = UIEdgeInsetsMake(0,5,0,-5)
        
    return button
    
    }()
    
    //懒加载scrollView
    private lazy var scrollView: UIScrollView = {
        
        let scrollView = UIScrollView()
        //关闭弹簧效果
        scrollView.bounces = false
        //开启分页
        scrollView.pagingEnabled = true
        //隐藏水平方向的滚动条
        scrollView.showsHorizontalScrollIndicator = false
        
        //代理
        scrollView.delegate = self
        
        return scrollView
    }()
    
    //懒加载pageControl
    private lazy var pageControl: UIPageControl = {
        let pageControl = UIPageControl()
        //避免小点点还可以单独点，要关闭用户交互
        pageControl.userInteractionEnabled = false
        
        //四个小点点
        pageControl.numberOfPages = 4
        
        //设置默认颜色和选中时的颜色
        pageControl.currentPageIndicatorTintColor = UIColor.orangeColor()
        pageControl.pageIndicatorTintColor = UIColor.blackColor()
        
        
        return pageControl
    }()
    
}
    //设置pageView跟随scrollView改变
extension TDNewfeatureViewController: UIScrollViewDelegate {
        
    func scrollViewDidScroll(scrollView: UIScrollView) {
            
          let pageNum = Int(scrollView.contentOffset.x / scrollView.frame.width + 0.5)
        
            pageControl.currentPage = pageNum
            
        }
        
        
    }




